package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.mgb.model.Product;
import com.macro.mall.mgb.model.ProductCategory;
import com.macro.mall.portal.domain.HomeContentResult;
import com.macro.mall.portal.domain.HomeProductDto;
import com.macro.mall.portal.domain.ProductCategoryDto;
import com.macro.mall.portal.domain.SimpleProductDTO;
import com.macro.mall.portal.service.HomeService;
import com.macro.mall.portal.util.GenericConverter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangwang
 * @version 1.0
 * @description 首页内容管理
 * @date 2025/7/25 18:25
 */
@RestController
@RequestMapping("/home")
@Tag(name = "HomeController", description = "首页内容管理接口")
public class HomeController {

    @Autowired
    private HomeService homeService;

    @Operation(summary = "首页内容信息展示")
    @GetMapping("/content")
    public CommonResult<HomeContentResult> content() {
        HomeContentResult contentResult = homeService.content();
        return CommonResult.success(contentResult);
    }

    @Operation(summary = "分页获取推荐商品")
    @GetMapping("/recommendProductList")
    public CommonResult<List<SimpleProductDTO>> recommendProductList(@RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<Product> productList = homeService.recommendProductList(pageSize, pageNum);
        List<SimpleProductDTO> productDTOS = GenericConverter.convertList(productList,SimpleProductDTO.class);
        return CommonResult.success(productDTOS);
    }

    @Operation(summary = "获取首页商品分类")
    @GetMapping("/productCateList/{parentId}")
    public CommonResult<List<ProductCategoryDto>> getProductCateList(@PathVariable Long parentId) {
        List<ProductCategoryDto> productCategoryList = homeService.getProductCateList(parentId);
        return CommonResult.success(productCategoryList);
    }

    @Operation(summary = "分页获取人气推荐商品")
    @GetMapping("/hotProductList")
    public CommonResult<List<HomeProductDto>> hotProductList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize) {
        List<HomeProductDto> productList = homeService.hotProductList(pageNum,pageSize);
        return CommonResult.success(productList);
    }

    @Operation(summary = "分页获取新品推荐商品")
    @GetMapping("/newProductList")
    public CommonResult<List<HomeProductDto>> newProductList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize) {
        List<HomeProductDto> productList = homeService.newProductList(pageNum,pageSize);
        return CommonResult.success(productList);
    }
}
